package com.byCreation.Profile;

import java.io.File;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.byCreation.Control.Control;
import com.byCreation.t_globe.LoginActivity;
import com.byCreation.t_globe.MainActivity;
import com.byCreation.t_globe.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.squareup.picasso.Picasso;

import FileIO.ReadWrite;
import FileIO.SaveImage;
import Myclass.ImgURL;
import Parser.HttpGetJson;
import Parser.SendToBD;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class MySettingActivity extends Activity {

	private Switch Switch01, Switch02, Switch03;

	
	// ===============GoogleAnalytics==============
	private GoogleAnalytics mGaInstance;
	private Tracker mGaTracker;
	// =============================================
	// ===================================googleAnalytics=============================
@Override
public void onStart() {
	super.onStart();
	// The rest of your onStart() code.
	EasyTracker.getInstance().activityStart(this); // Add this method.
	mGaTracker.sendView("MySetting Screen");

}

@Override
public void onStop() {
	super.onStop();
	// The rest of your onStop() code.
	EasyTracker.getInstance().activityStop(this); // Add this method.

}

// =====================================================================
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_my_setting);

		
		// =============================GoogleAnalytics=========================
		mGaInstance = GoogleAnalytics.getInstance(this);
		mGaTracker = mGaInstance.getTracker(Control.IDGoogleanalytics);
		
		
		Switch02 = (Switch) findViewById(R.id.Switch02);
		Switch02.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				// TODO Auto-generated method stub
				AudioManager amanager;
				amanager = (AudioManager) getSystemService(AUDIO_SERVICE);

				Toast.makeText(MySettingActivity.this, "Sound :" + isChecked,
						1000).show();

				// //turn off sound, disable notifications
				// amanager.setStreamMute(AudioManager.STREAM_SYSTEM,
				// isChecked);
				// Log.i("STREAM_SYSTEM", "Set to "+isChecked);
				// //notifications
				// amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION,
				// isChecked);
				// Log.i("STREAM_NOTIFICATION", "Set to "+isChecked);
				// //alarm
				// amanager.setStreamMute(AudioManager.STREAM_ALARM, isChecked);
				// Log.i("STREAM_ALARM", "Set to "+isChecked);
				// //ringer
				// amanager.setStreamMute(AudioManager.STREAM_RING, isChecked);
				// Log.i("STREAM_RING", "Set to "+isChecked);
				// //media
				// amanager.setStreamMute(AudioManager.STREAM_MUSIC, isChecked);
				// Log.i("STREAM_MUSIC", "Set to "+isChecked);

			}

		});

		findViewById(R.id.img_close).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();

			}
		});

		findViewById(R.id.lay_yourprofile).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						startActivity(new Intent(getBaseContext(),
								MyprofileActivity.class));
					}
				});

		findViewById(R.id.t_globeapp).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getBaseContext(),
						TglobeAppActivity.class));
			}
		});

		findViewById(R.id.lay_pass).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_pass).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(),"", 200).show();

				ShowDialog_Password(0);

			}
		});

		// findViewById(R.id.lay_grade).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_grade).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(),"", 200).show();
			}
		});

		// findViewById(R.id.lay_payment).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_payment).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		// findViewById(R.id.lay_distant).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_distant).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		// (R.id.lay_sendnotification).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_sendnotification).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		findViewById(R.id.lay_sound).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_sound).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "", 200).show();
			}
		});

		// findViewById(R.id.lay_vibra).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_vibra).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(),"", 200).show();
			}
		});

		// findViewById(R.id.lay_location).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_location).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		findViewById(R.id.lay_clearbookmark).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_clearbookmark).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ClearType = "bookmark";
						new Load_ClearData().execute();
					}
				});

		findViewById(R.id.lay_clearhistory).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_clearhistory).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ClearType = "history";
						new Load_ClearData().execute();
					}
				});

		findViewById(R.id.lay_clearnote).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_clearnote).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ClearType = "note";
						new Load_ClearData().execute();
					}
				});

		// findViewById(R.id.lay_help).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_help).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(),"", 200).show();
			}
		});

		// findViewById(R.id.lay_feedback).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_feedback).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		findViewById(R.id.lay_notice).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_notice).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getBaseContext(), NoticeActivity.class));
			}
		});

		findViewById(R.id.lay_cache).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_cache).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getBaseContext(),"", 200).show();
				
				clearApplicationData();
				
				
			}
		});

		findViewById(R.id.lay_abount).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_abount).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				ShowDialog_About();
				Toast.makeText(getBaseContext(), "", 200).show();
			}
		});

		findViewById(R.id.lay_current).setBackgroundResource(
				R.drawable.custom_button_setting);
		findViewById(R.id.lay_current).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						try {
							Toast.makeText(
									getBaseContext(),
									"application version : "
											+ MySettingActivity.this
													.getPackageManager()
													.getPackageInfo(
															MySettingActivity.this
																	.getPackageName(),
															0).versionName,
									1200).show();
						} catch (NameNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

		// findViewById(R.id.lay_service).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_service).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

		// findViewById(R.id.lay_privacy).setBackgroundResource(R.drawable.custom_button_setting);
		findViewById(R.id.lay_privacy).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Toast.makeText(getBaseContext(),"", 200).show();
					}
				});

	}

	public void ShowDialog_About()
	{
		final Dialog dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
		//dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
		//dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		dialog.setContentView(R.layout.dialog_abount);
        dialog.setCancelable(true);
        
        
        ImageView imageView1=(ImageView)dialog.findViewById(R.id.imageView1);
        Picasso.with(getApplicationContext()).load("http://www.freestyle-travel.com/pictures/lib/2014/05/13/4a83e75cc10e129e34fecc70ece6d729.jpg").into(imageView1);
        
        
        
        
        
        dialog.show();
        
     
      
        }
	
	
        
        
	
	
	private String ClearType = "history";// (history , bookmark , note)

	class Load_ClearData extends AsyncTask<Object, Integer, Object> {

		private ProgressDialog mProgressDialog;
		private String title, content, images, date, link, result, desc;
		private boolean Cancle = false;

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			Cancle = true;
			super.onCancelled();
		}

		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			try {
				JSONObject jsonObj = null;
				jsonObj = new JSONObject(HttpGetJson.GetJson(
						Control.getAPI_ALLClear(ClearType, Control.UserName),
						Control.user, Control.pass));

				desc = jsonObj.getString("desc");

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPreExecute() {

			// =====================ProgressDialog==========================
			mProgressDialog = new ProgressDialog(MySettingActivity.this);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setMessage("Loading......");
			mProgressDialog.setCancelable(true);

			if (mProgressDialog != null)
				mProgressDialog.show();

			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");

		}

		@Override
		protected void onPostExecute(Object result) {

			if (!Cancle) {

				Toast.makeText(MySettingActivity.this, desc, 1800).show();

				if (mProgressDialog != null)
					mProgressDialog.hide();
			}

		}

	}

	private Dialog dialog;

	public void ShowDialog_Password(int i) {

		Log.e("dg", " ShowDialog id=" + i);
		dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
		// dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
		// dialog.getWindow().getAttributes().windowAnimations =
		// R.style.DialogAnimation;
		dialog.setContentView(R.layout.lay_dialog_password);

		final EditText edt_password = (EditText) dialog
				.findViewById(R.id.edt_password);
		final EditText edt_confirmpassword = (EditText) dialog
				.findViewById(R.id.edt_confirmpassword);
		final EditText edt_newpassword = (EditText) dialog
				.findViewById(R.id.edt_newpassword);

		dialog.findViewById(R.id.img_newpass).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						if (edt_newpassword
								.getText()
								.toString()
								.equals(edt_confirmpassword.getText()
										.toString())) {
							if (edt_password.getText().toString()
									.equals(Control.UserPassword)) {
								// CallApi
								newpassword = Control.UserPassword;
								new NewPassword().execute();
							} else {
								Toast.makeText(MySettingActivity.this,
										"not match", 2000).show();

								// Toast.makeText(MySettingActivity.this,"not match1"+Control.UserPassword+"  :  "+edt_password.getText().toString(),
								// 2000).show();
							}

						} else {

							Toast.makeText(MySettingActivity.this, "not match",
									2000).show();

						}

					}
				});

		dialog.setCancelable(true);
		dialog.show();
	}

	private String newpassword = "";

	class NewPassword extends AsyncTask<Object, Integer, Object> {

		ProgressDialog mProgressDialog;
		boolean isCancle = false;
		String respone = "";

		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			try {
				ArrayList<String> arrayList_parametor = new ArrayList<String>();
				ArrayList<String> arrayList_value = new ArrayList<String>();

				arrayList_parametor.add("username");
				arrayList_value.add(Control.UserName);

				arrayList_parametor.add("passwd");
				arrayList_value.add(newpassword);

				SendToBD sd = new SendToBD(MySettingActivity.this);
				respone = sd.send(Control.user, Control.pass,
						Control.getAPI_CHANGE_PASSWORD(), arrayList_parametor,
						arrayList_value);

				JSONObject jsonObj;
				jsonObj = new JSONObject(respone);

				try {
					respone = jsonObj.getString("desc");
				} catch (Exception ex) {
				}
				;

			} catch (JSONException e) {
				e.printStackTrace();
				respone = e.getMessage();
			} catch (Exception e) {
				e.printStackTrace();
				respone = e.getMessage();
			}

			return null;
		}

		@Override
		protected void onPreExecute() {

			mProgressDialog = new ProgressDialog(MySettingActivity.this);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setMessage(" Change Password......");
			mProgressDialog.setCancelable(true);

			if (mProgressDialog != null)
				mProgressDialog.show();

			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");

		}

		@Override
		protected void onPostExecute(Object result) {

			if (!isCancle) {
				Toast.makeText(MySettingActivity.this, respone, 2000).show();
				if (respone.equals("password change success") && dialog != null)
					;
				dialog.dismiss();
			}

			if (mProgressDialog != null)
				mProgressDialog.dismiss();

		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			isCancle = true;
			super.onCancelled();
		}
	}

	public void clearApplicationData() {
		File cache = getCacheDir();
		File appDir = new File(cache.getParent());
		if (appDir.exists()) {
			String[] children = appDir.list();
			for (String s : children) {
				if (!s.equals("lib")) {
					deleteDir(new File(appDir, s));
					Log.i("TAG",
							"**************** File /data/data/APP_PACKAGE/" + s
									+ " DELETED *******************");
					
					
					Toast.makeText(this,"clearApplicationData", 1200).show();

				}
			}
		}
	}

	public static boolean deleteDir(File dir) {
		
		if (dir != null && dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			
			}
		}

		return dir.delete();
	}

}
